package cc.rengu.redp.flowable.bizrule.rule;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 表单提交校验规则
 */
public abstract class FormSubmitRule implements BusiRule {

    protected static final Logger logger = LoggerFactory.getLogger(FormSubmitRule.class);

    protected final static String RULE_PREFIX = "FSRule"; // form summit rule

    @Override
    public abstract RuleResult validateData(JSONObject validateJson);

    @Override
    public abstract RuleResult checkSuitable(JSONObject validateJson);

    @Override
    public abstract JSONObject getValidateData(JSONObject requestJson);

    @Override
    public void dataPreHandle(JSONObject validateJson) {
    }

    @Override
    public String getRulePrefix(){
        return RULE_PREFIX;
    }

    /**
     * 检查校验规则时所用的字段是否无法获取
     * (用于规则内的必须具备的条件字段，如缺失，属于内部错误，表示规则应用的程序存在问题，用户无法解决）
     * @param validateJson
     * @param elements
     * @return
     */
    protected RuleResult verifyElementIsNull(JSONObject validateJson, String... elements) {
        for (String element : elements) {
            if (validateJson.getString(element) == null) {
                return RuleResult.buildFailureResult("内部错误：无法获取校验字段" + element);
            }
        }

        return RuleResult.buildSuccessResult();
    }
}
